<!--
	作者：offline
	时间：2018-11-28
	描述：未发布采购订单列表
-->
<template>
	<div class="m-view-unPublishedPurOrder m-window" ref="elementId">
		<div class="m-condition" v-if="diaShow">
			<el-row :gutter="10">
				<el-form
					:model="ruleForm"
					ref="ruleForm"
					label-width="70px"
					:inline-message="true"
					label-position="left"
				>
					<el-col :span="7">
						<el-form-item label="公司" class="rightItem">
							<el-select
								value-key="id"
								v-model="selCompanyCode"
								placeholder="请选择公司"
								@change="companyChange"
								clearable
								ref="content"
								filterable
							>
								<el-option
									v-for="item in comOptions"
									:key="item.id"
									:label="item.companyName"
									:value="item"
								></el-option>
							</el-select>
						</el-form-item>
					</el-col>
                    <el-col :span="6">
						<el-form-item label="订单日期" class="rightItem">
							<el-date-picker
								v-model="applySDate"
								type="date"
								:placeholder="placeholder.start"
								value-format="yyyy-MM-dd"
								:clearable="false"
								:editable="false"
								:picker-options="po1"
							></el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :span="5">
						<el-date-picker
							v-model="applyEDate"
							type="date"
							:placeholder="placeholder.end"
							value-format="yyyy-MM-dd"
							:clearable="false"
							:editable="false"
							:picker-options="po2"
						></el-date-picker>
					</el-col>
					<el-col :span="6">
						<el-form-item label="订单号" class="rightItem">
							<el-input v-model="purOrd" placeholder="请输入采购订单号" clearable maxlength="12"></el-input>
						</el-form-item>
					</el-col>
				</el-form>
			</el-row>
			<el-row :gutter="10">
				<el-form
					:model="ruleForm"
					ref="ruleForm"
					label-width="70px"
					:inline-message="true"
					label-position="left"
				>
                    <el-col :span="7">
						<el-form-item label="供应商" class="rightItem">
							<el-input
								placeholder="请选择供应商"
								v-model="selCusName"
								clearable
								@focus="showSupplierDialog"
								readonly
								@mouseenter.native="mouseEnter"
								@mouseleave.native="mounseLeave"
							>
								<el-button slot="append" icon="el-icon-search" @click="showSupplierDialog"></el-button>
								<span slot="suffix" @click="inputClearSon" class="m-cleanInput" v-show="inputClearBut">
									<i class="el-input_icon el-icon-circle-close el-input__clear"></i>
								</span>
							</el-input>
						</el-form-item>
					</el-col>
                    <el-col :span="6">
						<el-form-item label="采购组" class="rightItem">
							<el-input v-model="purGroup" placeholder="请输入采购组" clearable maxlength="50"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="5">
						<el-form-item label="采购组织" class="rightItem">
							<el-select v-model="selPurOrgCode" placeholder="请选择采购组织" clearable @change="getPurOrgInfo">
								<el-option
									v-for="item in purOrgOptions"
									:key="item.purOrgCode"
									:label="item.purOrgName"
									:value="item.purOrgCode"
								></el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-form>
				<!-- <el-col :span="4" >
					<el-select v-model="ordStatus" placeholder="请选择订单状态" @change="orderStatusChange">
						<el-option v-for="item in ordStatusOptions" :key="item.value" :label="item.label" :value="item.value" ></el-option>
					</el-select>
				</el-col>-->
				<el-col :span="2" :offset="4">
					<el-button type="primary" @click="search(1)" :disabled="btnDis">查询</el-button>
				</el-col>
			</el-row>
		</div>
		<div class="m-agTable m-bottom m-top2" v-if="diaShow">
			<ag-grid-vue
				class="m-agTableContent ag-theme-balham"
				:gridOptions="gridOptions"
				:columnDefs="columnDefs"
				v-loading="loading"
				rowSelection="single"
			></ag-grid-vue>
		</div>
		<div class="m-page" v-if="diaShow">
			<el-pagination
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				@prev-click="handlePrevClick"
				@next-click="handleNextClick"
				:current-page="currentPage"
				:page-sizes="[30,50,100,150,200]"
				:page-size="pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="total"
			></el-pagination>
		</div>

		<!--弹框-->
		<el-dialog
			title="供应商信息"
			:visible.sync="srmSupplierDialogVisible"
			width="50%"
			:before-close="srmSupplierDialogClose"
			v-dialogDrag
			:close-on-click-modal="false"
			:close-on-press-escape="false"
		>
			<srmSupplierInfo ref="srmSupplierInfo" :layout="layout" @setSrmSupplierInfo="setSrmSupplierInfo"></srmSupplierInfo>
		</el-dialog>

		<div class="m-window" v-if="componentsShow">
			<purOrderDetailsComponents
				@cancelComponents="cancelComponents"
				:getClickValue="getClickValue"
				:ordStatus="ordStatus"
			></purOrderDetailsComponents>
		</div>
	</div>
</template>

<script>
import _Api from "@/api";
import localeText from "@/i18n/agGridText";
import srmSupplierInfo from "@/components/srmSupplierInfo.vue";
import purOrderDetailsComponents from "@/views/purchase/purOrderDetailsComponents.vue"
import serviceTime from "@/public/serverTime.js";
import authInfo from "@/public/authInfo.js";
import buttonLink from "@/components/buttonLink.js";
import { orderType } from "@/public/enum.js";
import constant from "@/public/constant";
import gridOptions from 'lesso-common/public/gridOptions';
export default {
	data () {
		return {
			ruleForm: {},
			btnDis: false,
			diaShow: true,
			selCompanyCode: "",
			comOptions: [],
			selCusName: "",
			selCusCode: "",
			inputClearBut: false,
			purOrd: "",
			applySDate: "",
			applyEDate: "",
			selPurOrgCode: "",
			purOrgOptions: [],
			purGroup: "",
			loading: false,
			currentPage: 1,
			pageSize: 30,
			total: 0,
			dialogSize: document.body.clientWidth,
			layout: {},
			srmSupplierDialogVisible: false,
			componentsShow: false,
			ordStatusOptions: [
				{ value: 1, label: "未发布" },
				{ value: 2, label: "已发布" }],
			ordStatus: "",
			dateClear: false,
			getClickValue: {},
			current: 0,
			textChange: 0,
			po1: {
				disabledDate: this.disabledDate1
			},
			po2: {
				disabledDate: this.disabledDate2
			},
			// 日期控件默认值 
			placeholder: {
				start: '',
				end: ''
			}
		}
	},
	watch: {
		dialogSize (val) {
			this.dialogSize = val;
			//   this.gridOptions.api.sizeColumnsToFit();
		},
		selCusName (val) {
			this.currentPage = 1;
			this.search();
		},
		selCompanyCode (val) {
			this.currentPage = 1;
			this.search();
		},
		applySDate (value) {
			if (value == null)
				this.applySDate = "";
			this.current = 1;
		},
		applyEDate (value) {
			if (value == null)
				this.applyEDate = "";
			this.current = 1;
		},
		selPurOrgCode (val) {
			//采购组织
			this.current = 1;
		},
		purOrd (val) {
			//订单号
			if (val)
				this.textChange = 1;
			else
				this.textChange = 0;
		},
		purGroup (val) {
			//采购组
			if (val)
				this.textChange = 1;
			else
				this.textChange = 0;
		}
	},
	methods: {
		disabledDate1 (time) {
			if (this.applyEDate)
				return time.getTime() > new Date(this.applyEDate).getTime();
		},
		disabledDate2 (time) {
			if (this.applySDate)
				return time.getTime() < (new Date(this.applySDate).getTime() - 3600 * 1000 * 24);
		},
		setClickValue (params) {
			this.getClickValue = params.data;
			this.pageLayOut = "edit";
			this.diaShow = false;
			this.componentsShow = true;
		},
		orderStatusChange (params) {
			//订单状态
			// if(params === 1){
			// 	this.gridOptions.columnApi.setColumnVisible("state",false);
			// 	this.btnShow = false;
			// }else if(params === 2){
			// 	this.btnShow = true;
			// 	this.gridOptions.columnApi.setColumnVisible("state",true);
			// }
			// this.gridOptions.api.sizeColumnsToFit();
		},
		companyChange (params) {
			if (params) {
				this.layout.companyCode = params.companyCode;
				this.layout.auth = true;
				// this.layout.filterByUser = true;
				if (this.selCusCode)
					this.selCusCode = "";
				if (this.selCusName)
					this.selCusName = "";
			} else {
				this.layout = {};
				this.selCusName = "";
			}
			//				this.currentPage = 1;
		},
		showSupplierDialog () {
			var that = this;
			if (!this.layout.companyCode) {
				this.$alert("提示：请选择公司！", {
					showClose: false,
					confirmButtonText: "确定",
					customClass: "alertPrompt"
				});
				this.$refs.content.focus();
				return;
			} else {
				this.srmSupplierDialogVisible = true;
				this.$nextTick(_ => {
					that.$refs.srmSupplierInfo.search();
				});
			}
		},
		inputClearSon () {
			this.selCusCode = "";
			this.selCusName = "";
			this.inputClearBut = false;
		},
		mouseEnter () {
			if (this.selCusName != "")
				this.inputClearBut = true;
		},
		mounseLeave () {
			if (this.selCusName != "")
				this.inputClearBut = false;
		},
		getPurOrgInfo () {

		},
		search (flag) {
			var that = this;
			if (this.applySDate != "" && this.applyEDate != "" && this.applySDate && this.applyEDate) {
				if (this.applySDate > this.applyEDate) {
					this.$alert("提示：开始日期不能大于结束日期", {
						showClose: false,
						confirmButtonText: "确定",
						customClass: "alertPrompt"
					});
					return;
				}
			}
			if (!this.selCompanyCode) {
				this.$alert("提示：请选择公司", {
					showClose: false,
					confirmButtonText: "确定",
					customClass: "alertPrompt"
				});
				return;
			}
			// if(!this.selCusCode){
			// 	this.$alert("提示：请选择供应商", {
			// 		showClose: false,
			// 		confirmButtonText: "确定",
			// 		customClass: "alertPrompt"
			// 	});
			// 	return;
			// }
			if ((this.applySDate == "" || this.applySDate == null) && (this.applyEDate == "" || this.applyEDate == null)) {
				this.$alert("提示：请选择时间段", {
					showClose: false,
					confirmButtonText: "确定",
					customClass: "alertPrompt"
				});
				return;
			}
			if (!this.selPurOrgCode) {
				this.$alert("提示：请选择采购组织", {
					showClose: false,
					confirmButtonText: "确定",
					customClass: "alertPrompt"
				});
				return;
			}
			var obj = {};
			obj.companyCode = this.selCompanyCode.companyCode;
			if (this.selCusCode)
				obj.supplierCode = this.selCusCode;
			if (this.purOrd != "")
				obj.purOrdNo = this.purOrd;

			if (this.applySDate != "" && this.applySDate != null)
				obj.Begin_Date = this.applySDate;
			else if ((this.applySDate == "" || this.applySDate == null) && (this.applyEDate != "" && this.applyEDate != null))
				obj.Begin_Date = this.applyEDate;

			if (this.applyEDate != "" && this.applyEDate != null)
				obj.End_Date = this.applyEDate;
			else if ((this.applySDate != "" && this.applySDate != null) && (this.applyEDate == "" || this.applyEDate == null))
				obj.End_Date = this.applySDate;

			if (this.selPurOrgCode != "")
				obj.purOrgCode = this.selPurOrgCode;
			if (this.purGroup != "")
				obj.purGroup = this.purGroup;

			if (this.flag === 1)
				this.currentPage = 1;
			obj.currentPage = this.currentPage;//this.currentPage;
			obj.pageSize = this.pageSize;
			this.loading = true;
			this.btnDis = true;
			_Api.getPurNPublishList(obj).then(function (response) {
				that.btnDis = false;
				that.current = 0;
				that.textChange = 0;
				var data = response.data;
				if (data.code == "1") {
					if (data.total > 0) {
						that.gridOptions.api.setRowData(data.rows.splice(0));
						that.total = data.total;
					} else {
						that.gridOptions.api.setRowData([]);
						that.total = 0;
					}
					that.loading = false;
				} else {
					that.$alert(data.info, {
						showClose: false,
						confirmButton: "确定",
						customClass: "alertFailure"
					});
					that.gridOptions.api.setRowData([]);
					that.loading = false;
					that.total = 0;
				}
			}).catch((err) => {
				that.$alert(err.data.message, {
					showClose: false,
					confirmButton: "确定",
					customClass: "alertFailure"
				});
				that.total = 0;
				that.current = 0;
				that.textChange = 0;
				that.loading = false;
				that.btnDis = false;
			})
		},
		getContextMenuItems () {
			return [];
		},
		handleSizeChange (params) {
			this.currentPage = 1;
			this.pageSize = params;
			this.search();
		},
		handleCurrentChange (params) {
			if (this.textChange === 1 || this.current === 1)
				this.currentPage = 1;
			else
				this.currentPage = params;
			this.search();
		},
		handlePrevClick (params) {
			if (this.textChange === 1 || this.current === 1)
				this.currentPage = 1;
			else
				this.currentPage = params;
			// this.handleCurrentChange(this.currentPage);
		},
		handleNextClick (params) {
			if (this.textChange === 1 || this.current === 1)
				this.currentPage = 1;
			else
				this.currentPage = params;
			// this.handleCurrentChange(this.currentPage);
		},
		setSrmSupplierInfo (msg) {
			if (msg.length > 0) {
				this.srmSupplierDialogVisible = false;
				this.selCusCode = msg[0].supplierCode;
				this.selCusName = msg[0].supplierName;
			}
		},
		srmSupplierDialogClose (done) {
			this.$refs.srmSupplierInfo.keyWord = "";
			this.$refs.srmSupplierInfo.selSupplierOptions.api.setQuickFilter(this.$refs.srmSupplierInfo.keyWord);
			done();
		},
		cancelComponents (msg) {
			if (msg) {
				// var that = this;
				this.componentsShow = false;
				this.diaShow = true;
				this.btnShow = false;
				this.search();
				// this.$nextTick(_ => {
				//   that.gridOptions.api.sizeColumnsToFit();
				//   that.gridOptions.api.hideOverlay();
				// });
			}
		},
		takeBack () {
			var rows = this.gridOptions.api.getSelectedRows();
			if (rows.length <= 0) {
				this.$alert("提示：请选择需要收回的数据", {
					showClose: false,
					confirmButtonText: "确定",
					customClass: "alertPrompt"
				});
				return;
			}
		},
		// 获取采购组织
		getPurOrgOptions () {
			let params = {
				"userType": constant.accountType
			}
			_Api.getMstPurchaseorgList(params).then(res => {
				let data = res.data
				if (data.code == 1) {
					this.purOrgOptions = data.rows
					if (Array.isArray(this.purOrgOptions) && this.purOrgOptions.length > 0) {
						this.selPurOrgCode = this.purOrgOptions[0].purOrgCode
					}
				} else {
					this.$alert(data.info, {
						showClose: false,
						confirmButton: "确定",
						customClass: "alertFailure"
					})
				}
			})
				.catch(err => {
					this.$alert(err.data.message, {
						showClose: false,
						confirmButton: "确定",
						customClass: "alertFailure"
					});
				})
		}
	},
	beforeMount () {
		this.gridOptions = Object.assign({
			context: {
				componentParent: this
			},
			localeText: localeText,
			suppressRowClickSelection: true,
			//   getContextMenuItems: this.getContextMenuItems,
			enableColResize: true,
			onGridReady: function ($event) {
				// that.gridOptions.api.sizeColumnsToFit();
			}
		},gridOptions())
		this.columnDefs = [
			{ headerName: "订单号", field: "purOrdNo", cellRenderer: buttonLink, width: 150, checkboxSelection: true },
			{
				headerName: "订单类型", field: "purOrdType", width: 120, cellRenderer: function (params) {
					if (params.value) {
						for (let a in orderType) {
							if (orderType[a].value == params.value)
								return orderType[a].label;
						}
					}
				}
			},
			{ headerName: "供应商编号", field: "supplierCode", width: 120 },
			{ headerName: "供应商名称", field: "supplierName", width: 200 },
			{
				headerName: "订单日期",
				field: "purOrdDate",
				width: 100,
				cellRenderer: function (params) {
					if (params.value)
						return new Date(Date.parse(params.value.replace(/-/g, "/"))).Format("yyyy-MM-dd");
				}
			},
			{ headerName: "公司编号", field: "companyCode", width: 100 },
			{ headerName: "公司名称", field: "companyName", width: 200 },
			{ headerName: "采购组", field: "purGroupName", width: 80 }
		];// purGroup
		let tips = ['companyName', 'supplierName', 'matName', 'creator']
		this.columnDefs.forEach(col => {
			if (tips.indexOf(col.field) > -1) {
				col.tooltipField = col.field
			}
		})
	},
	mounted () {
		var that = this;
		window.onresize = () => {
			return (() => {
				that.dialogSize = document.body.clientWidth;
			})();
		}
		this.getPurOrgOptions()
		this.gridOptions.api.hideOverlay();
		authInfo.getAuthInfo().then((authInfo) => {
			that.comOptions = authInfo;
		});
		// this.purOrgOptions = purOrgCode;
		this.ordStatus = 1;//未发布
		serviceTime.getServiceTime().then((serviceTime) => {
			that.applyEDate = serviceTime.Format("yyyy-MM-dd");
			that.applySDate = new Date(serviceTime.getTime() - 3600 * 1000 * 24 * 7).Format('yyyy-MM-dd');
			that.placeholder.start = "开始日期"
			that.placeholder.end = "结束日期"
		});
		if (this.$global.user.userTypeId != "4") {
			this.purGroup = this.$global.user.loginName;
		}
		//设置默认日期范围	最近7天
		//			this.applyEDate = (new Date()).Format("yyyy-MM-dd");
		//			this.applySDate =  new Date((new Date()).getTime() - 604800000).Format("yyyy-MM-dd");
	},
	components: {
		srmSupplierInfo,
		purOrderDetailsComponents
	},
	created: function () {
	}
}
</script>